package com.prs.crm.action.crm;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.crm.CodeNumber;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.CustomerAddress;
import com.prs.crm.domain.crm.CustomerAgent;
import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.crm.CustomerSource;
import com.prs.crm.domain.crm.CustomerTrade;
import com.prs.crm.domain.crm.CustomerType;
import com.prs.crm.domain.crm.DocumenTary;
import com.prs.crm.domain.crm.DocumenTaryStyle;
import com.prs.crm.domain.crm.Linkman;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.crm.CodeNumberService;
import com.prs.crm.service.crm.CustomerAddressService;
import com.prs.crm.service.crm.CustomerAgentService;
import com.prs.crm.service.crm.CustomerLevelService;
import com.prs.crm.service.crm.CustomerService;
import com.prs.crm.service.crm.CustomerSourceService;
import com.prs.crm.service.crm.CustomerTradeService;
import com.prs.crm.service.crm.CustomerTypeService;
import com.prs.crm.service.crm.DocumenTaryService;
import com.prs.crm.service.crm.DocumenTaryStyleService;
import com.prs.crm.service.crm.LinkmanService;
/**
 * 代理商action
 * @author zhoupeng 2012-2-15
 *
 */
public class CustomerAgentAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private CustomerAgent customerAgent;
	private Customer customer;
	private DocumenTary documenTary;
	private Integer[] customerIds;
	private Linkman linkman;
	private CodeNumber codeNumber;
	private Person person;
	
	private CodeNumberService codeNumberService;
	private PaginationSupport pagination;
	private CustomerAgentService customerAgentService;
	private CustomerService customerService;
	private LinkmanService linkmanService;
	private DocumenTaryService documenTaryService;
	private List<CustomerTrade> trades;
	private List<CustomerLevel> levels;
	private List<CustomerType> types;
	private List<CustomerAddress> addresses;
	private List<CustomerSource> sources;
	private List<Customer> customers;  //关联客户
	private List<Linkman> linkmans;  //联系人
	private List<DocumenTary> tarys;  //跟单
	private List<DocumenTaryStyle> styles;
	
	private DocumenTaryStyleService documenTaryStyleService;
	private CustomerTradeService customerTradeService;
	private CustomerLevelService customerLevelService;
	private CustomerTypeService customerTypeService;
	private CustomerAddressService customerAddressService;
	private CustomerSourceService customerSourceService;
	//默认值设置
	private String value;
	private String key;
	private String valueTrade;
	private String keyTrade;
	private String valueSource;
	private String keySource;
	
	@Action("listCustomerAgent")
	public String listCustomerAgent(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CustomerAgent.class);
		person = AppContext.getAccountContext().currentUser();
		if(person.getRole().getName()!="系统管理员"){
			criteria.add(Restrictions.or(Restrictions.like("shareperson", "%"+person.getResume().getName()+"%"), Restrictions.like("person", "%"+person.getResume().getName()+"%")));
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCustomerAgentService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("selectCustomerAgent")
	public String selectCustomerAgent(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CustomerAgent.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCustomerAgentService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editCustomerAgent")
	public String editCustomerAgent(){
		if(customerIds!=null){
			this.customerAgent=this.customerAgentService.get(customerIds[0]);
		}else{
			this.codeNumber=this.codeNumberService.getCodeByState("代理商");
		}
		this.trades = this.getCustomerTradeService().getAllCustomerTrade();
		if(customerAgent==null){
			for(int i=0;i<trades.size();i++){
				CustomerTrade t=trades.get(i);
				if(t.getRemark()!=null){
					this.valueTrade=t.getName();
					this.keyTrade=t.getId().toString();
					trades.remove(t);
				}
			}
		}
		this.addresses=this.customerAddressService.getalladdress();
		//设置默认值
		if(customerAgent==null){
		for(int i=0;i<addresses.size();i++){
			CustomerAddress ad=addresses.get(i);
			if(ad.getRemark()!=null){
				this.value=ad.getName();
				this.key=ad.getId().toString();
				addresses.remove(ad);
			}
		}}
		this.sources=this.customerSourceService.getAllSources();
		if(customerAgent==null){
			for(int i=0;i<sources.size();i++){
				CustomerSource s=sources.get(i);
				if(s.getRemark()!=null){
					this.valueSource=s.getName();
					this.keySource=s.getId().toString();
					sources.remove(s);
				}
			}
		}
		return SUCCESS;
	}
	@Action("editCustomer2")
	public String edit2() {
		this.levels = this.getCustomerLevelService().getAllCustomerLevel();
		this.types = this.getCustomerTypeService().getAllCustomerType();
		this.trades = this.getCustomerTradeService().getAllCustomerTrade();
		
		//this.provinces = this.getProvinceService().getAll();
		return SUCCESS;
	}
	@Action("viewCustomerAgent")
	public String viewCustomerAgent(){
		if(customerIds!=null){
			this.customerAgent=this.customerAgentService.get(customerIds[0]);
		}
		this.customers=this.customerService.getCustomerByAgent(customerAgent);
		this.linkmans=this.linkmanService.getLinkmanByCustomerAgent(customerAgent);
		this.tarys=this.documenTaryService.getDocumenTaryByAgent(customerAgent);
		return SUCCESS;
	}
	@Action("editDocumenTary2")
	public String editDocumenTary2(){
		if(customerIds!=null){
			this.customerAgent=this.customerAgentService.get(customerIds[0]);
		}
		this.styles=this.documenTaryStyleService.getAllStyles();
		return SUCCESS;
	}
	@Action("editLinkmanByDL")
	public String editLinkmanByDL(){
		if(customerIds!=null){
			this.customerAgent=this.customerAgentService.get(customerIds[0]);
		}
		return SUCCESS;
	}
	@Action("removeCustomerAgent")
	public String removeCustomerAgent(){
		for(int i=0;i<customerIds.length;i++){
			CustomerAgent agent=this.customerAgentService.get(customerIds[i]);
			//查询代理商关联客户
			List<Customer> cuss=this.customerService.getCustomerByAgent(agent);
			if(cuss!=null){
				for(Customer cus:cuss){
					this.customerService.remove(cus);
				}
			}
			//查询代理商关联的联系人
			List<Linkman> lins=this.linkmanService.getLinkmanByCustomerAgent(agent);
			if(lins!=null){
				for(Linkman lin:lins){
					linkmanService.remove(lin);
				}
			}
			//查询代理商关联跟单
			List<DocumenTary> docs=this.documenTaryService.getDocumenTaryByAgent(agent);
			if(docs!=null){
				for(DocumenTary doc:docs){
					documenTaryService.remove(doc);
				}
			}
			customerAgentService.remove(agent);
		}
		return SUCCESS;
	}
	
	@Action(value = "saveCustomerAgent", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerAgent"),
			@Result(name = "error", type = "chain", location = "editCustomerAgent")})
	public String saveCustomerAgent(){
		if(customerAgent.getId()==null){
			this.customerAgentService.save(customerAgent);
			//编号
			String edit=this.codeNumber.getEdit();
			String sample=this.codeNumber.getSample();
			String[] ee=edit.split(",");
			String[] ss=sample.split(",");
			List<String> list=new ArrayList<String>();
			for(int i=0;i<ee.length;i++){
				String samp="";
				if(ee[i].equals("##")){
					int bb=Integer.parseInt(ss[i]);
					bb=bb+1;
					String b="";
					for(int a=0;a<ss[i].length()-String.valueOf(bb).length();a++){
						b=b+"0";
					}
					samp=b+bb;
					ss[i]=samp;
				}
				if(i==ee.length-1){
					list.add(ss[i]);
				}else{
					list.add(ss[i]+",");
				}
			}
			String d="";
			String q="";
			for(int i=0;i<list.size();i++){
				d=d+list.get(i);
			}
			String[] dd=d.split(",");
			for(int i=0;i<dd.length;i++){
				q=q+dd[i];
			}
			this.codeNumber.setSample(d);
			this.codeNumber.setCodeSample(q);
			int now=Integer.parseInt(codeNumber.getNowCode())+1;
			this.codeNumber.setNowCode(String.valueOf(now));
			this.codeNumberService.submit(codeNumber);
			this.setPrompt("录入代理商成功");
		}else{
			this.customerAgentService.submit(customerAgent);
			this.setPrompt("修改代理商成功");
		}
		return SUCCESS;
	}
	@Action(value = "save1", results = {
			@Result(name = "success", type = "redirect", location = "viewCustomerAgent?customerAgent=${customerAgent.getId()}"),
			@Result(name = "error", type = "chain", location = "editCustomerAgent")})
	public String save1(){
		this.customer.setCustomerAgent(customerAgent);
		customer.setState(Constrains.TASK_AUDITING);
		this.customerService.save(customer);
		this.setPrompt("创建客户成功");
		return SUCCESS;
	}
	@Action(value = "save2", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerAgent"),
			@Result(name = "error", type = "chain", location = "editCustomerAgent")})
	public String save2(){
		this.linkman.setCustomerAgent(customerAgent);
		this.linkmanService.save(linkman);
		this.setPrompt("创建联系人成功");
		return SUCCESS;
	}
	@Action(value = "saveLinkmanByAgent2", results = {
			@Result(name = "success", type = "redirect", location = "viewCustomerAgent?customerAgent=${customerAgent.getId()}"),
			@Result(name = "error", type = "chain", location = "editCustomerAgent")})
	public String saveLinkmanByAgent2(){
		this.linkman.setCustomerAgent(customerAgent);
		this.linkmanService.save(linkman);
		this.setPrompt("创建联系人成功");
		return SUCCESS;
	}
	@Action(value = "saveDocumenTary2", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerAgent"),
			@Result(name = "error", type = "chain", location = "editCustomerAgent")})
	public String saveDocumenTary2(){
		this.documenTary.setCustomerAgent(customerAgent);
		this.documenTaryService.save(documenTary);
		this.setPrompt("创建联系人成功");
		return SUCCESS;
	}
	@Action(value = "saveDocTary2", results = {
			@Result(name = "success", type = "redirect", location = "viewCustomerAgent?customerAgent=${customerAgent.getId()}"),
			@Result(name = "error", type = "chain", location = "editCustomerAgent")})
	public String saveDocTary2(){
		this.documenTary.setCustomerAgent(customerAgent);
		this.documenTaryService.save(documenTary);
		this.setPrompt("创建联系人成功");
		return SUCCESS;
	}
	

	public Integer[] getCustomerIds() {
		return customerIds;
	}
	public void setCustomerIds(Integer[] customerIds) {
		this.customerIds = customerIds;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public CustomerAgentService getCustomerAgentService() {
		return customerAgentService;
	}

	public void setCustomerAgentService(CustomerAgentService customerAgentService) {
		this.customerAgentService = customerAgentService;
	}
	public List<CustomerTrade> getTrades() {
		return trades;
	}
	public void setTrades(List<CustomerTrade> trades) {
		this.trades = trades;
	}
	public CustomerTradeService getCustomerTradeService() {
		return customerTradeService;
	}
	public void setCustomerTradeService(CustomerTradeService customerTradeService) {
		this.customerTradeService = customerTradeService;
	}
	public List<CustomerLevel> getLevels() {
		return levels;
	}
	public void setLevels(List<CustomerLevel> levels) {
		this.levels = levels;
	}
	public List<CustomerType> getTypes() {
		return types;
	}
	public void setTypes(List<CustomerType> types) {
		this.types = types;
	}
	public CustomerLevelService getCustomerLevelService() {
		return customerLevelService;
	}
	public void setCustomerLevelService(CustomerLevelService customerLevelService) {
		this.customerLevelService = customerLevelService;
	}
	public CustomerTypeService getCustomerTypeService() {
		return customerTypeService;
	}
	public void setCustomerTypeService(CustomerTypeService customerTypeService) {
		this.customerTypeService = customerTypeService;
	}
	public CustomerService getCustomerService() {
		return customerService;
	}
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public Linkman getLinkman() {
		return linkman;
	}
	public void setLinkman(Linkman linkman) {
		this.linkman = linkman;
	}
	public LinkmanService getLinkmanService() {
		return linkmanService;
	}
	public void setLinkmanService(LinkmanService linkmanService) {
		this.linkmanService = linkmanService;
	}
	public DocumenTary getDocumenTary() {
		return documenTary;
	}
	public void setDocumenTary(DocumenTary documenTary) {
		this.documenTary = documenTary;
	}
	public DocumenTaryService getDocumenTaryService() {
		return documenTaryService;
	}
	public void setDocumenTaryService(DocumenTaryService documenTaryService) {
		this.documenTaryService = documenTaryService;
	}
	public List<Customer> getCustomers() {
		return customers;
	}
	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}
	public List<Linkman> getLinkmans() {
		return linkmans;
	}
	public void setLinkmans(List<Linkman> linkmans) {
		this.linkmans = linkmans;
	}
	public List<DocumenTary> getTarys() {
		return tarys;
	}
	public void setTarys(List<DocumenTary> tarys) {
		this.tarys = tarys;
	}
	public CustomerAgent getCustomerAgent() {
		return customerAgent;
	}
	public void setCustomerAgent(CustomerAgent customerAgent) {
		this.customerAgent = customerAgent;
	}
	public List<CustomerAddress> getAddresses() {
		return addresses;
	}
	public void setAddresses(List<CustomerAddress> addresses) {
		this.addresses = addresses;
	}
	public CustomerAddressService getCustomerAddressService() {
		return customerAddressService;
	}
	public void setCustomerAddressService(
			CustomerAddressService customerAddressService) {
		this.customerAddressService = customerAddressService;
	}
	public List<CustomerSource> getSources() {
		return sources;
	}
	public void setSources(List<CustomerSource> sources) {
		this.sources = sources;
	}
	public CustomerSourceService getCustomerSourceService() {
		return customerSourceService;
	}
	public void setCustomerSourceService(CustomerSourceService customerSourceService) {
		this.customerSourceService = customerSourceService;
	}
	public List<DocumenTaryStyle> getStyles() {
		return styles;
	}
	public void setStyles(List<DocumenTaryStyle> styles) {
		this.styles = styles;
	}
	public DocumenTaryStyleService getDocumenTaryStyleService() {
		return documenTaryStyleService;
	}
	public void setDocumenTaryStyleService(
			DocumenTaryStyleService documenTaryStyleService) {
		this.documenTaryStyleService = documenTaryStyleService;
	}
	public CodeNumber getCodeNumber() {
		return codeNumber;
	}
	public void setCodeNumber(CodeNumber codeNumber) {
		this.codeNumber = codeNumber;
	}
	public CodeNumberService getCodeNumberService() {
		return codeNumberService;
	}
	public void setCodeNumberService(CodeNumberService codeNumberService) {
		this.codeNumberService = codeNumberService;
	}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getValueTrade() {
		return valueTrade;
	}
	public void setValueTrade(String valueTrade) {
		this.valueTrade = valueTrade;
	}
	public String getKeyTrade() {
		return keyTrade;
	}
	public void setKeyTrade(String keyTrade) {
		this.keyTrade = keyTrade;
	}
	public String getValueSource() {
		return valueSource;
	}
	public void setValueSource(String valueSource) {
		this.valueSource = valueSource;
	}
	public String getKeySource() {
		return keySource;
	}
	public void setKeySource(String keySource) {
		this.keySource = keySource;
	}
	
}
